<template>
	<view>
		<view class="job-dtl-box">
			<view class="common-tips-box">
				<view class="t1">{{id ? '编辑':'添加'}}求职期望</view>
				<view class="t2">求职期望的不同，推荐的职位也会不同</view>
			</view>
			<view class="dtl-box">
				<view class="job-box">
					<view class="tit">期望职位</view>
					<navigator hover-class="hoverClass2" url="/pages/recruit/position-model/index">
						<view :class="{'tips-txt': detail.position}">
							{{ detail.position || '请选择期望职位'}}
							<text class="iconfontPT iconxiayibu pull-right"></text>
						</view>
					</navigator>
				</view>
				<view class="job-box">
					<view class="tit">期望行业</view>
					<navigator hover-class="hoverClass2" url="/pages/recruit/industry-model/index">
						<view>
							{{ detail.industry || '不限'}}
							<text class="iconfontPT iconxiayibu pull-right"></text>
						</view>
					</navigator>
				</view>
				<view class="job-box">
					<view class="tit">期望城市</view>
					<navigator hover-class="hoverClass2" url="/pages/recruit/city-model/index">
						<view>
							{{ detail.city || '不限'}}
							<text class="iconfontPT iconxiayibu pull-right"></text>
						</view>
					</navigator>
				</view>
				<view class="job-box">
					<view class="tit">期望薪资</view>
					<picker mode="multiSelector" @change="bindMultiPickerChange" @columnchange="bindMultiPickerColumnChange" :value="multiIndex"
					 :range="multiArray">
						<view :class="{ 'picker clear': true, 'tips-txt': !detail.salary }">
							{{ detail.salary || '请选择期望薪资' }}
							<text class="iconfontPT iconxiayibu pull-right"></text>
						</view>
					</picker>
				</view>
			</view>
		</view>
		<btnTpl @confirmEvent="submitDataFn">保存</btnTpl>
	</view>
</template>

<script>
	import btnTpl from '../../components/recruit/btn-tpl.vue'
	import {
		getExpectDtlInfo,
		setExpectDtlInfo,
	} from '../../../apis/recruit/index.js'

	export default {
		data() {
			return {
				multiIndex: [0, 0],
				multiArray: [],
				id: '',
				detail: {
					job: '',
					position: '',
					industry: '',
					city: '',
					salary: '',
					s_salary: 0,
					e_salary: 0
				}
			}
		},
		onLoad(options) {
			let list = []
			for (let i = 1; i <= 30; i++) {
				list.push(`${i}k`)
			}

			this.multiArray = [
				['面议'].concat(list), []
			]

			if (options.id) {
				this.id = options.id

				this.initDataFn();
			}
		},
		components: {
			btnTpl
		},
		methods: {
			initDataFn() {
				let _this = this;
				getExpectDtlInfo({
					id: _this.data.id
				}).then(res => {
					if (res.code == 1) {
						_this.setData({
							detail: res.data
						})
					}
				})
			},
			//提交求职期望
			submitDataFn() {
				let _this = this;
				let params = _this.detail;

				_this.verificationFn(params, ok => {
					if (ok) {
						params.type = 1;
						_this.$loading.show()
						setExpectDtlInfo(params).then(res => {
							if (res.code == 1) {
								// $Message({
								// 	content: _this.data.id ? '修改成功' : '提交成功',
								// 	type: 'success'
								// });

								setTimeout(() => {
									var pages = getCurrentPages();
									if (pages.length > 1) {
										//上一个页面实例对象 
										var prevPage = pages[pages.length - 2];
										//关键在这里,这里面是触发上个界面的方法 
										prevPage.$vm.getExpectDtlInfo() // 123
									}
									_this.$loading.hide()
									uni.navigateBack({
										delta: 1 // delta: 1返回上一级（A 页面）    delta: 2返回上两级
									})
								}, 2000)
							}
						})
					}
				})
			},
			//非空字段验证
			verificationFn(obj, cb) {
				let _this = this;
				if (!obj.position) {
					_this.errorSHowModelFn('请选择期望职位', 'info');
					cb(false);
					return;
				}
				if (!obj.salary) {
					_this.errorSHowModelFn('请选择期望薪资', 'info');
					cb(false);
					return;
				}
				cb(true);
			},
			// 错误异常弱提示 公共方法
			errorSHowModelFn(msg, type) {
				$Message({
					content: msg || '服务器开小差了',
					type: type
				});
			},
			bindMultiPickerChange(e) {
				let multiArray = this.multiArray,
					k1 = e.detail.value[0] != 0 ? multiArray[0][e.detail.value[0]].substr(0, multiArray[0][e.detail.value[0]].length -
						1) : multiArray[0][e.detail.value[0]],
					k2 = multiArray[1][e.detail.value[1]] || '';
				this.detail.salary = k1 + (k2 ? '-' + k2 : '')
				this.detail.s_salary = k1
				this.detail.e_salary = k2.replace('k', '')
			},
			bindMultiPickerColumnChange(e) {
				let {
					multiIndex,
					multiArray
				} = this, {
					column,
					value
				} = e.detail;

				multiIndex[column] = value;

				column === 0 && value === 0 && (multiArray[1] = [])
				column === 0 && (multiArray[1] = multiArray[0].slice(value + 1))

				this.$set(this.multiIndex, multiIndex)
				this.$set(this.multiArray, multiArray)
			},
		}
	}
</script>

<style lang="less">
	.job-dtl-box {
		padding: 0 30rpx 0;
	}

	.tips-txt {
		color: #989898;
	}

	.dtl-box {
		margin-top: 22rpx;
	}

	.job-box {
		padding: 38rpx 0 0;
		box-shadow: 0 1rpx 0.5rpx -1rpx #CCCCCC;
	}

	.job-box {
		.tit {
			font-size: 24rpx;
			line-height: 24rpx;
		}
	}

	.job-box {

		navigator,
		picker {
			font-size: 30rpx;
			line-height: 30rpx;
			padding-top: 18rpx;
			padding-bottom: 38rpx;
		}
	}

	.iconxiayibu {
		font-size: 24rpx;
		color: #999;
	}
</style>
